package com.wanxi.util;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
public class LoginFilter extends HttpFilter {
    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
//        可以将乱码处理放入过滤器
//        得到前端访问后台的地址
        String path = req.getServletPath();
//  在访问Java后台时，只需判断路径是否以back开头的即可
        if (path.startsWith("/admin/")) {
            String name = (String) req.getSession().getAttribute("managerName");
            if (name == null) {
//    表示灭有登录 //跳转到登录页面
                res.sendRedirect("/login");
            } else {
                chain.doFilter(req, res);//放行
            }
        } else {
            chain.doFilter(req, res);//放行
        }
    }
}
